package myzzyl.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import myzzyl.constants.ErrorConstants;
import myzzyl.domain.dto.NursingLevelDto;
import myzzyl.domain.pojo.NursingLevel;
import myzzyl.domain.pojo.NursingLevel;
import myzzyl.domain.pojo.NursingPlan;
import myzzyl.domain.vo.NursingLevelVo;
import myzzyl.domain.vo.NursingPlanVo;
import myzzyl.exception.BusinessException;
import myzzyl.response.PageResponse;
import myzzyl.service.NursingLevelService;
import myzzyl.mapper.NursingLevelMapper;
import myzzyl.service.NursingPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 33249
* @description 针对表【nursing_level(护理等级表)】的数据库操作Service实现
* @createDate 2025-08-10 10:20:36
*/
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel>
    implements NursingLevelService{


    @Autowired
    private NursingPlanService nursingPlanService;

    /**
     * 分页获取所有护理等级
     * @param name 等级名称
     * @param status 等级状态
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 返回值
     */
    @Override
    public PageResponse<NursingLevelVo> getAll(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<NursingLevel> page = new Page<>(pageNum, pageSize);

        // 添加排序字段
        page.addOrder(OrderItem.desc("create_time"));

        // 自定义分页查询
        IPage<NursingLevelVo> nursingLevelVoIPage = baseMapper.selectNursingLevelVo(page, name, status);

        // 封装分页响应格式对象返回
        return new PageResponse<>(nursingLevelVoIPage.getPages(), nursingLevelVoIPage.getTotal(),
                nursingLevelVoIPage.getRecords());
    }


    /**
     * 获取护理等级详情
     * @param id 护理等级id
     * @return 返回值
     */
    @Override
    public NursingLevelVo getInfo(Integer id) {
        // 查询护理等级是否存在
        NursingLevel nursingLevel = getById(id);
        if(nursingLevel == null) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_IS_NOT_EXIST);
        }

        // 根据护理计划id查询护理计划
        NursingPlanVo nursingPlanVo = nursingPlanService.getInfo(nursingLevel.getPlanId());

        // 构造Vo对象返回
        NursingLevelVo nursingLevelVo = new NursingLevelVo();
        BeanUtils.copyProperties(nursingLevel, nursingLevelVo);
        nursingLevelVo.setPlanName(nursingPlanVo.getPlanName());
        return nursingLevelVo;
    }


    /**
     * 新增护理等级
     * @param nursingLevelDto 护理等级表单请求包装类
     * @return 返回值
     */
    @Override
    public boolean add(NursingLevelDto nursingLevelDto) {
        // 判断护理等级名是否为空
        if (StrUtil.isBlank(nursingLevelDto.getName())) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_NAME_IS_NULL);
        }

        // 查询护理等级是否已经存在
        if (lambdaQuery().eq(NursingLevel::getName, nursingLevelDto.getName()).exists()) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_IS_ALREADY_EXIST);
        }

        // 新增护理等级
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);

        return save(nursingLevel);
    }


    /**
     * 编辑护理等级
     * @param nursingLevelDto 护理等级表单请求包装类
     * @return 返回值
     */
    @Override
    public boolean edit(NursingLevelDto nursingLevelDto) {
        // 判断护理等级名是否为空
        if (StrUtil.isBlank(nursingLevelDto.getName())) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_NAME_IS_NULL);
        }

        // 查询护理等级是否已经存在（除去自己）
        if (lambdaQuery()
                .eq(NursingLevel::getName, nursingLevelDto.getName())
                .ne(NursingLevel::getId, nursingLevelDto.getId())
                .exists()) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_IS_ALREADY_EXIST);
        }

        // 更新护理等级
        return lambdaUpdate()
                .set(StrUtil.isNotBlank(nursingLevelDto.getName()), NursingLevel::getName, nursingLevelDto.getName())
                .set(StrUtil.isNotBlank(nursingLevelDto.getDescription()), NursingLevel::getDescription, nursingLevelDto.getDescription())
                .set(nursingLevelDto.getFee() != null, NursingLevel::getFee, nursingLevelDto.getFee())
                .set(nursingLevelDto.getStatus() != null, NursingLevel::getStatus, nursingLevelDto.getStatus())
                .set(nursingLevelDto.getPlanId() != null, NursingLevel::getPlanId, nursingLevelDto.getPlanId())
                .eq(NursingLevel::getId, nursingLevelDto.getId())
                .update();
    }


    /**
     * 删除护理等级
     * @param id 护理等级id
     * @return 返回值
     */
    @Override
    public boolean deleteNursingLevelById(Integer id) {
        // 查询护理等级是否存在
        if(!lambdaQuery().eq(NursingLevel::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_IS_NOT_EXIST);
        }

        // 删除护理等级
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 启用或禁用护理等级
     * @param id 护理等级id
     * @param status 需要更新的护理等级状态
     * @return 返回值
     */
    @Override
    public boolean enableOrDisableNursingLevel(Integer id, Integer status) {
        // 查询护理等级是否存在
        if(!lambdaQuery().eq(NursingLevel::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.NURSING_LEVEL_IS_NOT_EXIST);
        }

        // 更新护理等级状态
        return lambdaUpdate()
                .set(status != null, NursingLevel::getStatus, status)
                .eq(NursingLevel::getId, id)
                .update();
    }


    /**
     * 获取所有护理等级
     * @return 返回值
     */
    @Override
    public List<NursingLevelVo> listAll() {
        // 复用已有的分页查询接口，但是不传入分页相关参数
        Page<NursingLevel> page = new Page<>();

        // 添加排序字段
        page.addOrder(OrderItem.desc("create_time"));

        // 直接调用已有的分页查询
        return baseMapper.selectNursingLevelVo(page, null, null).getRecords();
    }


}




